﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Common;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Orm.Model;

namespace OpenSoftStore.Client.UIL.ManageCommon
{
    public partial class FormTreeSelecter : Form
    {
        public TreeLogic TreeBll;
        public string TypeId;
        public string TypeText;
        public List<TreeNode> TreeNodes;

        public Dictionary<string, string> Pys = new Dictionary<string, string>();
        public string PySelect = "";

        public FormTreeSelecter()
        {
            InitializeComponent();
            TreeBll=new TreeLogic();
            TreeNodes=new List<TreeNode>();
        }

        private void SetPy(string key, string text)
        {
            if (!Pys.Any(m => m.Key == key))
            {
                Pys.Add(key, text);
            }
        }

        private string GetPy(string pySelect)
        {
            var py = Pys.FirstOrDefault(m => m.Key.Contains(pySelect));
            var count = Pys.Count(m => m.Key.Contains(pySelect));
            //if (count == 1) PySelect = "";
            if (string.IsNullOrEmpty(py.Key))
            {
                PySelect = "";
                return "";
            }
            return Pys[py.Key];
        }

        private void CheckAllChildNodes(TreeNode treeNode, List<TreeNode> treeNodes)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                //var tree = new TreeNode { Name = node.Text, Id = node.Name };
                treeNodes.Add(node);
                if (node.Nodes.Count > 0)
                {
                    this.CheckAllChildNodes(node, treeNodes);
                }
            }
        }

        public void GetTreeByType(string rootType,string code="")
        {
            TreeBll.SetTreeNode(TreeBll.GetTreeByType(rootType, code), trTree);
            trTree.ExpandAll();
            CheckAllChildNodes(trTree.Nodes[0], TreeNodes);
            if (Pys.Count == 0)
            {
                foreach (var treeNode in TreeNodes)
                {
                    var text = treeNode.Text;
                    SetPy(CnToPy.CapitalCovert(text), text);
                }
            }
        }

        private void FormTreeSelecter_Load(object sender, EventArgs e)
        {

        }


        private void trTree_DoubleClick(object sender, EventArgs e)
        {
            //if (trTree.SelectedNode.Index == 0) return;
            if (trTree.SelectedNode == null || trTree.SelectedNode.Level == 0) return;
            TypeId = trTree.SelectedNode.Name;
            TypeText = trTree.SelectedNode.Text;
            DialogResult = DialogResult.OK;
        }

        private void trTree_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            TypeId = trTree.SelectedNode.Name;
            TypeText = trTree.SelectedNode.Text;
            DialogResult = DialogResult.OK;
        }

        private void trTree_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Subtract || e.KeyCode == Keys.Back)
            {
                PySelect = "";
            }
            if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z))
            {
                PySelect += e.KeyCode;
            }
            if (!string.IsNullOrEmpty(PySelect))
            {
                var text = GetPy(PySelect);
                if (!string.IsNullOrEmpty(text)) SelectTreeView(trTree, text);
            }
        }
        private void SelectTreeView(TreeView treeView, string selectStr)
        {
            treeView.Focus();
            foreach (var treeNode in TreeNodes)
            {
                if (treeNode.Text == selectStr)
                {
                    treeView.SelectedNode = treeNode;//选中
                    return;
                }
            }
        }
    }
}
